using System;
using Castle.MonoRail.Framework;
using System.Collections;
using Pic.Model;
using NHibernate.Expression;


namespace Pic.Web.Components.Pic
{
    public class PicHomeList : ViewComponent
    {
        private string _viewType;
        [ViewComponentParam]
        public string ViewType
        {
            get { return _viewType; }
            set { _viewType = value; }
        }

        private IList dataSource;

        public override bool SupportsSection(string name)
        {
            return true;
        }

        public void InitDataSource()
        {
            Order[] orderNew = new Order[1] { Order.Desc("Pic_AlbumID") };
            Order[] orderHit = new Order[2] { Order.Desc("Hits"), Order.Desc("Pic_AlbumID") };
            Order[] orderComm = new Order[2] { Order.Desc("CommNum"), Order.Desc("Pic_AlbumID") };
            Order[] orderGood = new Order[2] { Order.Desc("FavNum"), Order.Desc("Pic_AlbumID") };
            Order[] order;
            order = orderNew;
            if (_viewType == "Hit") order = orderHit;
            if (_viewType == "Comm") order = orderComm;
            if (_viewType == "Good") order = orderGood;

            dataSource = Pic_Album.SlicedFindAll(0, 12, order, Expression.Eq("Status", (byte)1));

        }

        public override void Render()
        {
            InitDataSource();
            if (dataSource.Count != 0)
            {
                PropertyBag.Add("Albums", dataSource);
                Context.RenderSection("list");
            }
            else {
                Context.RenderSection("empty");
            }
        }
    }
}